import 'dart:io';
import 'package:dio/dio.dart';
import 'package:elephant_fish/http/core/hi_net.dart';
import 'package:elephant_fish/http/request/base_request.dart';
import 'package:elephant_fish/http/request/modify_phone_request.dart';
import 'package:elephant_fish/model/user_info_entity.dart';
import 'package:elephant_fish/page/reset_password_page.dart';
import 'package:elephant_fish/util/TxUtils.dart';
import 'package:elephant_fish/util/jhPickerTool.dart';
import 'package:elephant_fish/util/size_util.dart';
import 'package:elephant_fish/util/string_util.dart';
import 'package:elephant_fish/widget/picture_wall.dart';
import 'package:elephant_fish/widget/text_fild.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:image_picker/image_picker.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'login_page.dart';

class UpdateInfoPage extends StatefulWidget {
  final Map arguments;

  const UpdateInfoPage({Key key, this.arguments}) : super(key: key);

  @override
  _UpdateInfoPageState createState() => _UpdateInfoPageState();
}

class _UpdateInfoPageState extends State<UpdateInfoPage> {
  File images;
  List<File> image = List();
  TextEditingController _nameController = TextEditingController();
  TextEditingController _vcodeController = TextEditingController();
  //分别定义两个输入框的焦点 用于切换焦点
  final FocusNode _nodeName = FocusNode();
  final FocusNode _nodeVCode = FocusNode();
  bool resetPhont = false;
  String phone;
  String code;
  bool _isClick = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getInitPic();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Color(0xff140c3d),
      appBar: AppBar(
        backgroundColor: Color(0xff140c3d),
        centerTitle: true,
        leading: GestureDetector(
          child: Icon(
            Icons.arrow_back_ios,
            size: SizeUtil.px(32),
            color: Color(0xfff3f3f3),
          ),
          onTap: () {
            Navigator.pop(context);
          },
        ),
        title: Text(
          '编辑资料',
          style: TextStyle(fontSize: SizeUtil.px(32), color: Color(0xfff5f5f5)),
        ),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [
            SizedBox(
              height: SizeUtil.px(20),
            ),
            Align(
              alignment: Alignment.topCenter,
              child: Container(
                width: SizeUtil.px(710),
                height: SizeUtil.px(160),
                decoration: BoxDecoration(
                    color: Color(0xff2b244d),
                    borderRadius:
                    BorderRadius.all(Radius.circular(SizeUtil.px(12)))),
                child: InkWell(
                  child: Stack(
                    children: [
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Padding(
                          padding: EdgeInsets.only(
                            left: SizeUtil.px(24),
                          ),
                          child: Text(
                            '头像',
                            style: TextStyle(
                                fontSize: SizeUtil.px(29),
                                color: Color(0xfff3f3f3)),
                          ),
                        ),
                      ),
                      Align(
                        alignment: Alignment.centerRight,
                        child: Padding(
                            padding: EdgeInsets.only(
                              right: SizeUtil.px(24),
                            ),
                            child: InkWell(
                              child: images == null
                                  ? Container(
                                width: SizeUtil.px(100),
                                height: SizeUtil.px(100),
                                child: CircleAvatar(
                                  radius: SizeUtil.px(35),
                                  backgroundImage: NetworkImage(widget
                                      .arguments['userInfo'] ==
                                      null
                                      ? 'https://imgcache.qq.com/operation/dianshi/other/5.ca48acfebc4dfb68c6c463c9f33e60cb8d7c9565.png'
                                      : widget.arguments['userInfo']
                                      .avatar ==
                                      ""
                                      ? 'https://imgcache.qq.com/operation/dianshi/other/5.ca48acfebc4dfb68c6c463c9f33e60cb8d7c9565.png'
                                      : 'http://119.28.131.94:82' +
                                      widget.arguments['userInfo']
                                          .avatar),
                                ),
                              )
                                  : _generateImages(),
                              onTap: () {
                                _pickImage();
                              },
                            )),
                      )
                    ],
                  ),
                ),
              ),
            ),
            SizedBox(
              height: SizeUtil.px(31),
            ),
            Align(
              alignment: Alignment.topCenter,
              child: Container(
                width: SizeUtil.px(710),
                height: SizeUtil.px(160),
                decoration: BoxDecoration(
                    color: Color(0xff2b244d),
                    borderRadius:
                    BorderRadius.all(Radius.circular(SizeUtil.px(12)))),
                child: InkWell(
                  child: Stack(
                    children: [
                      Align(
                        alignment: Alignment.centerLeft,
                        child: Padding(
                          padding: EdgeInsets.only(
                            left: SizeUtil.px(24),
                          ),
                          child: Text(
                            '照片墙',
                            style: TextStyle(
                                fontSize: SizeUtil.px(29),
                                color: Color(0xfff3f3f3)),
                          ),
                        ),
                      ),
                      image == null ?
                          Container(width: 0,height: 0,):
                          Align(
                            alignment: Alignment.topLeft,
                            child: Padding(
                                padding: EdgeInsets.only(
                                  left: SizeUtil.px(120),
                                ),
                                child:  ListView.builder(
                                      scrollDirection: Axis.horizontal,
                                       itemCount: image.length,
                                       itemBuilder: (context,index){
                                         return Container(
                                             width: 45,
                                             height: 35,
                                             decoration: BoxDecoration(
                                               borderRadius: BorderRadius.all(Radius.circular(5)),
                                             ),
                                             child:  InkWell(
                                               child:
                                                   index == 0 ?
                                                   widget.arguments['userInfo'].photoWall.photo1 != null ?
                                                   PictureWall(image1: widget.arguments['userInfo'].photoWall.photo1,):
                                               PictureWall(image: image[index],) :   index == 1 ?
                                                   widget.arguments['userInfo'].photoWall.photo2 != null ?
                                                   PictureWall(image1: widget.arguments['userInfo'].photoWall.photo2,):
                                                   PictureWall(image: image[index],) :   index == 2 ?
                                                   widget.arguments['userInfo'].photoWall.photo3 != null ?
                                                   PictureWall(image1: widget.arguments['userInfo'].photoWall.photo3,):
                                                   PictureWall(image: image[index],) :   index == 3 ?
                                                   widget.arguments['userInfo'].photoWall.photo4 != null ?
                                                   PictureWall(image1: widget.arguments['userInfo'].photoWall.photo4,):
                                                   PictureWall(image: image[index],) :
                                                   widget.arguments['userInfo'].photoWall.photo5 != null ?
                                                   PictureWall(image1: widget.arguments['userInfo'].photoWall.photo5,):
                                                   PictureWall(image: image[index],),
                                               onTap: (){
                                                 _pickUpdateImageWall(index);
                                               },
                                           ),
                                         );
                                       }
                                   )
                                ),
                          )  ,
                      Align(
                        alignment: Alignment.centerRight,
                        child: Padding(
                            padding: EdgeInsets.only(
                              right: SizeUtil.px(24),
                            ),
                            child: InkWell(
                              child:  Container(
                                width: SizeUtil.px(100),
                                height: SizeUtil.px(100),
                                child:  Image.asset('assets/images/add_icon.png'),

                              ),
                              onTap: () {
                                _pickImageWall(image.length);
                              },
                            )),
                      )
                    ],
                  ),
                ),
              ),
            ),
            SizedBox(
              height: SizeUtil.px(31),
            ),
            Align(
              alignment: Alignment.topCenter,
              child: Container(
                width: SizeUtil.px(710),
                decoration: BoxDecoration(
                    color: Color(0xff2b244d),
                    borderRadius:
                    BorderRadius.all(Radius.circular(SizeUtil.px(12)))),
                child: Column(
                  children: [
                    SizedBox(
                      height: SizeUtil.px(40),
                    ),
                    Stack(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Padding(
                            padding: EdgeInsets.only(
                              left: SizeUtil.px(24),
                            ),
                            child: Text(
                              '昵称',
                              style: TextStyle(
                                  fontSize: SizeUtil.px(29),
                                  color: Color(0xfff3f3f3)),
                            ),
                          ),
                        ),
                        InkWell(
                          child: Align(
                            alignment: Alignment.centerRight,
                            child: Padding(
                              padding: EdgeInsets.only(
                                right: SizeUtil.px(75),
                              ),
                              child: Text(
                                widget.arguments['userInfo'] == null
                                    ? '今天吃什么'
                                    : widget.arguments['userInfo'].nickname == ''
                                    ? '今天吃什么'
                                    : widget.arguments['userInfo'].nickname,
                                style: TextStyle(
                                    fontSize: SizeUtil.px(29),
                                    color: Color(0xfff3f3f3)),
                              ),
                            ),
                          ),
                          onTap: () {
                            _showNickname();
                          },
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Padding(
                              padding: EdgeInsets.only(
                                  right: SizeUtil.px(26), top: SizeUtil.px(10)),
                              child: Icon(
                                Icons.arrow_forward_ios,
                                size: SizeUtil.px(29),
                                color: Color(0xffc4c4c4),
                              )),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: SizeUtil.px(70),
                    ),
                    Stack(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Padding(
                            padding: EdgeInsets.only(
                              left: SizeUtil.px(24),
                            ),
                            child: Text(
                              'ID',
                              style: TextStyle(
                                  fontSize: SizeUtil.px(29),
                                  color: Color(0xfff3f3f3)),
                            ),
                          ),
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Padding(
                            padding: EdgeInsets.only(
                              right: SizeUtil.px(26),
                            ),
                            child: Text(
                              widget.arguments['userInfo'].idNumber.toString() ==
                                  ''
                                  ? '60952354'
                                  : widget.arguments['userInfo'].idNumber
                                  .toString(),
                              style: TextStyle(
                                  fontSize: SizeUtil.px(29),
                                  color: Color(0xfff3f3f3)),
                            ),
                          ),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: SizeUtil.px(70),
                    ),
                    Stack(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Padding(
                            padding: EdgeInsets.only(
                              left: SizeUtil.px(24),
                            ),
                            child: Text(
                              '个性签名',
                              style: TextStyle(
                                  fontSize: SizeUtil.px(29),
                                  color: Color(0xfff3f3f3)),
                            ),
                          ),
                        ),
                        InkWell(
                          child: Align(
                            alignment: Alignment.centerRight,
                            child: Padding(
                              padding: EdgeInsets.only(
                                right: SizeUtil.px(75),
                              ),
                              child: Text(
                                widget.arguments['userInfo'] == null
                                    ? '暂无个性签名'
                                    : widget.arguments['userInfo'].signature == ''
                                    ? '暂无个性签名'
                                    : widget.arguments['userInfo'].signature,
                                style: TextStyle(
                                    fontSize: SizeUtil.px(29),
                                    color: Color(0xfff3f3f3)),
                              ),
                            ),
                          ),
                          onTap: () {
                            _showSignature();
                          },
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Padding(
                              padding: EdgeInsets.only(
                                  right: SizeUtil.px(26), top: SizeUtil.px(10)),
                              child: Icon(
                                Icons.arrow_forward_ios,
                                size: SizeUtil.px(29),
                                color: Color(0xffc4c4c4),
                              )),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: SizeUtil.px(70),
                    ),
                    Stack(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Padding(
                            padding: EdgeInsets.only(
                              left: SizeUtil.px(24),
                            ),
                            child: Text(
                              '性别',
                              style: TextStyle(
                                  fontSize: SizeUtil.px(29),
                                  color: Color(0xfff3f3f3)),
                            ),
                          ),
                        ),
                        InkWell(
                          child: Align(
                            alignment: Alignment.centerRight,
                            child: Padding(
                              padding: EdgeInsets.only(
                                right: SizeUtil.px(75),
                              ),
                              child: Text(
                                widget.arguments['userInfo'] == null
                                    ? '男'
                                    : widget.arguments['userInfo'].sex == 1
                                    ? '男'
                                    : '女',
                                style: TextStyle(
                                    fontSize: SizeUtil.px(29),
                                    color: Color(0xfff3f3f3)),
                              ),
                            ),
                          ),
                          onTap: () {
                            var aa = ["男", '女'];
                            JhPickerTool.showStringPicker(context, data: aa,
                                clickCallBack: (int index, var str) {
                                  print(index);
                                  setState(() {
                                    widget.arguments['userInfo'].sex = index + 1;
                                  });
                                });
                          },
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Padding(
                              padding: EdgeInsets.only(
                                  right: SizeUtil.px(26), top: SizeUtil.px(10)),
                              child: Icon(
                                Icons.arrow_forward_ios,
                                size: SizeUtil.px(29),
                                color: Color(0xffc4c4c4),
                              )),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: SizeUtil.px(70),
                    ),
                    Stack(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Padding(
                            padding: EdgeInsets.only(
                              left: SizeUtil.px(24),
                            ),
                            child: Text(
                              '出生年月',
                              style: TextStyle(
                                  fontSize: SizeUtil.px(29),
                                  color: Color(0xfff3f3f3)),
                            ),
                          ),
                        ),
                        InkWell(
                          child: Align(
                            alignment: Alignment.centerRight,
                            child: Padding(
                              padding: EdgeInsets.only(
                                right: SizeUtil.px(75),
                              ),
                              child: Text(
                                widget.arguments['userInfo'] == null
                                    ? '设置'
                                    : widget.arguments['userInfo'].birthday == ''
                                    ? '设置'
                                    : widget.arguments['userInfo'].birthday,
                                style: TextStyle(
                                    fontSize: SizeUtil.px(29),
                                    color: Color(0xfff3f3f3)),
                              ),
                            ),
                          ),
                          onTap: (){
                            JhPickerTool.showDatePicker(context, clickCallback: (var str,var time){
                              var times = time.split(' ');
                              setState(() {
                                widget.arguments['userInfo'].birthday = times[0];
                              });
                            });
                          },
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Padding(
                              padding: EdgeInsets.only(
                                  right: SizeUtil.px(26), top: SizeUtil.px(10)),
                              child: Icon(
                                Icons.arrow_forward_ios,
                                size: SizeUtil.px(29),
                                color: Color(0xffc4c4c4),
                              )),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: SizeUtil.px(70),
                    ),
                    Stack(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Padding(
                            padding: EdgeInsets.only(
                              left: SizeUtil.px(24),
                            ),
                            child: Text(
                              '绑定手机',
                              style: TextStyle(
                                  fontSize: SizeUtil.px(29),
                                  color: Color(0xfff3f3f3)),
                            ),
                          ),
                        ),
                        InkWell(
                          child: Align(
                            alignment: Alignment.centerRight,
                            child: Padding(
                              padding: EdgeInsets.only(
                                right: SizeUtil.px(75),
                              ),
                              child: Text(
                                widget.arguments['userInfo'].phone
                                    .replaceFirst(new RegExp(r'\d{4}'), '****', 3),
                                style: TextStyle(
                                    fontSize: SizeUtil.px(29),
                                    color: Color(0xfff3f3f3)),
                              ),
                            ),
                          ),
                          onTap: (){
                            _showPhone();
                          },
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Padding(
                              padding: EdgeInsets.only(
                                  right: SizeUtil.px(26), top: SizeUtil.px(4)),
                              child: Icon(
                                Icons.arrow_forward_ios,
                                size: SizeUtil.px(29),
                                color: Color(0xffc4c4c4),
                              )),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: SizeUtil.px(70),
                    ),
                    Stack(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Padding(
                            padding: EdgeInsets.only(
                              left: SizeUtil.px(24),
                            ),
                            child: Text(
                              '密码',
                              style: TextStyle(
                                  fontSize: SizeUtil.px(29),
                                  color: Color(0xfff3f3f3)),
                            ),
                          ),
                        ),
                        InkWell(
                          child:   Align(
                            alignment: Alignment.centerRight,
                            child: Padding(
                              padding: EdgeInsets.only(
                                right: SizeUtil.px(75),
                              ),
                              child: Text(
                                '修改',
                                style: TextStyle(
                                    fontSize: SizeUtil.px(29),
                                    color: Color(0xfff3f3f3)),
                              ),
                            ),
                          ),
                          onTap: (){
                            Navigator.of(context).push( MaterialPageRoute(builder: (context)=>ResetPasswordPage()));
                          },
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Padding(
                              padding: EdgeInsets.only(
                                  right: SizeUtil.px(26), top: SizeUtil.px(10)),
                              child: Icon(
                                Icons.arrow_forward_ios,
                                size: SizeUtil.px(29),
                                color: Color(0xffc4c4c4),
                              )),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: SizeUtil.px(70),
                    ),
                    Stack(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Padding(
                            padding: EdgeInsets.only(
                              left: SizeUtil.px(24),
                            ),
                            child: Text(
                              '绑定微信',
                              style: TextStyle(
                                  fontSize: SizeUtil.px(29),
                                  color: Color(0xfff3f3f3)),
                            ),
                          ),
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Padding(
                            padding: EdgeInsets.only(
                              right: SizeUtil.px(75),
                            ),
                            child: Text(
                              '未绑定',
                              style: TextStyle(
                                  fontSize: SizeUtil.px(29),
                                  color: Color(0xfff3f3f3)),
                            ),
                          ),
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Padding(
                              padding: EdgeInsets.only(
                                  right: SizeUtil.px(26), top: SizeUtil.px(10)),
                              child: Icon(
                                Icons.arrow_forward_ios,
                                size: SizeUtil.px(29),
                                color: Color(0xffc4c4c4),
                              )),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: SizeUtil.px(70),
                    ),
                    Stack(
                      children: [
                        Align(
                          alignment: Alignment.centerLeft,
                          child: Padding(
                            padding: EdgeInsets.only(
                              left: SizeUtil.px(24),
                            ),
                            child: Text(
                              '绑定QQ',
                              style: TextStyle(
                                  fontSize: SizeUtil.px(29),
                                  color: Color(0xfff3f3f3)),
                            ),
                          ),
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Padding(
                            padding: EdgeInsets.only(
                              right: SizeUtil.px(75),
                            ),
                            child: Text(
                              '未绑定',
                              style: TextStyle(
                                  fontSize: SizeUtil.px(29),
                                  color: Color(0xfff3f3f3)),
                            ),
                          ),
                        ),
                        Align(
                          alignment: Alignment.centerRight,
                          child: Padding(
                              padding: EdgeInsets.only(
                                  right: SizeUtil.px(26), top: SizeUtil.px(10)),
                              child: Icon(
                                Icons.arrow_forward_ios,
                                size: SizeUtil.px(29),
                                color: Color(0xffc4c4c4),
                              )),
                        ),
                      ],
                    ),
                    SizedBox(
                      height: SizeUtil.px(43),
                    )
                  ],
                ),
              ),
            ),
            SizedBox(
              height: SizeUtil.px(68),
            ),
            InkWell(
              child: Align(
                alignment: Alignment.topCenter,
                child: Container(
                  width: SizeUtil.px(650),
                  height: SizeUtil.px(91),
                  decoration: BoxDecoration(
                    color: Color(0xffe62d6c),
                    borderRadius:
                    BorderRadius.all(Radius.circular(SizeUtil.px(46))),
                  ),
                  child: Center(
                    child: Text(
                      '确认修改',
                      style: TextStyle(
                          fontSize: SizeUtil.px(32), color: Color(0xfff9f9f9)),
                    ),
                  ),
                ),
              ),
              onTap: () {
                updateUserInfo();
              },
            )
          ],
        ),
      )
    );
  }

  Future getImageWall(bool isTakePhoto,int index) async {
    Navigator.pop(context);
    var _image;
    try {
      _image = await ImagePicker.pickImage(
          source: isTakePhoto ? ImageSource.camera : ImageSource.gallery);
      if (_image == null) {
        return;
      } else {
        setState(() {
          // _image = image;
         File file = File(_image.path);
         if(image.length > 4){
           TxUtils.showToast('上传数量不能超过5张', context);
         }else{
           image.add(file);
         }

        });
      }
    } catch (e) {
      print("模拟器不支持相机！");
    }
  }


  Future getUpdateImageWall(bool isTakePhoto,int index) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    Navigator.pop(context);
    var _image;
    try {
      _image = await ImagePicker.pickImage(
          source: isTakePhoto ? ImageSource.camera : ImageSource.gallery);
      if (_image == null) {
        return;
      } else {
        setState(() async {
          // _image = image;
         File file = File(_image.path);

           Map<String, dynamic> map = Map();
           FormData formData;
           if(index == 0){
             map['photo1'] = await MultipartFile.fromFile(file.path);
           }
         if(index == 1){
           map['photo2'] = await MultipartFile.fromFile(file.path);
         }
         if(index == 2){
           map['photo3'] = await MultipartFile.fromFile(file.path);
         }
         if(index == 3){
           map['photo4'] = await MultipartFile.fromFile(file.path);
         }
         if(index == 4){
           map['photo5'] = await MultipartFile.fromFile(file.path);
         }

           formData = FormData.fromMap(map);
           var dio = new Dio();
           dio.options.headers = {"usertoken": appToken};
           var response =
               await dio.post("http://119.28.131.94:82/app/user/photo/wall/edit", data: formData);
           String res = response.data.toString();
           var result = response.data;
           if (result['code'] == -1) {
             TxUtils.showErrorToast('登录失效，请重新登录', context);
             Navigator.pop(context);
             Navigator.of(context).push(MaterialPageRoute(
               builder: (context) {
                 return LoginPage(
                   arguments: {'loginCode': '-1'},
                 );
               },
             ));
           } else if (result['code'] == 0) {
             TxUtils.showToast(result['data'], context);
             Navigator.of(context).pop("refresh");
           }

         image[index] = file;

        });
      }
    } catch (e) {
      print("模拟器不支持相机！");
    }
  }

  Future getImage(bool isTakePhoto) async {
    Navigator.pop(context);
    var image;
    try {
      image = await ImagePicker.pickImage(
          source: isTakePhoto ? ImageSource.camera : ImageSource.gallery);
      if (image == null) {
        return;
      } else {
        setState(() {
          // _image = image;
          images = File(image.path);
        });
      }
    } catch (e) {
      print("模拟器不支持相机！");
    }
  }

  ///底部弹框
  _pickImage() {
    showModalBottomSheet(
        context: context,
        builder: (context) => Container(
              height: 160,
              child: Column(
                children: <Widget>[
                  _takePhotoItem('拍照', true),
                  _takePhotoItem('从相册选择', false),
                ],
              ),
            ));
  }

  ///底部弹框
  _pickImageWall(int index) {
    showModalBottomSheet(
        context: context,
        builder: (context) => Container(
          height: 160,
          child: Column(
            children: <Widget>[
              takePhotoItem('拍照', true,index),
              takePhotoItem('从相册选择', false,index),
            ],
          ),
        ));
  }
  ///底部弹框
  _pickUpdateImageWall(int index) {
    showModalBottomSheet(
        context: context,
        builder: (context) => Container(
          height: 160,
          child: Column(
            children: <Widget>[
              takeUpdatePhotoItem('拍照', true,index),
              takeUpdatePhotoItem('从相册选择', false,index),
            ],
          ),
        ));
  }

  takePhotoItem(String title, bool isTakePhoto,int index) {
    print(isTakePhoto);
    return GestureDetector(
      child: ListTile(
        leading: Icon(
          isTakePhoto ? Icons.camera_alt : Icons.photo_library,
        ),
        title: Text(title),
        onTap: () => getImageWall(isTakePhoto,index),
      ),
    );
  }
  takeUpdatePhotoItem(String title, bool isTakePhoto,int index) {
    print(isTakePhoto);
    return GestureDetector(
      child: ListTile(
        leading: Icon(
          isTakePhoto ? Icons.camera_alt : Icons.photo_library,
        ),
        title: Text(title),
        onTap: () => getUpdateImageWall(isTakePhoto,index),
      ),
    );
  }

  _takePhotoItem(String title, bool isTakePhoto) {
    print(isTakePhoto);
    return GestureDetector(
      child: ListTile(
        leading: Icon(
          isTakePhoto ? Icons.camera_alt : Icons.photo_library,
        ),
        title: Text(title),
        onTap: () => getImage(isTakePhoto),
      ),
    );
  }

  /// 封装图片面板
  _generateOneImages() {
    return Stack(
      children: <Widget>[
        ClipRRect(
          //圆角效果
          borderRadius: BorderRadius.circular(SizeUtil.px(50)),
          child: Image.file(images,
              width: SizeUtil.px(100),
              height: SizeUtil.px(100),
              fit: BoxFit.fill),
        ),
        Positioned(
          right: 5,
          top: 5,
          child: GestureDetector(
            onTap: () {
              setState(() {
                images = null;
              });
            },
            child: ClipOval(
              //圆角删除按钮
              child: Container(
                padding: EdgeInsets.all(5),
                decoration: BoxDecoration(color: Colors.black54),
                child: Icon(
                  Icons.close,
                  size: 20,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }

  /// 封装图片面板
  _generateImages() {
    return Stack(
      children: <Widget>[
        ClipRRect(
          //圆角效果
          borderRadius: BorderRadius.circular(SizeUtil.px(50)),
          child: Image.file(images,
              width: SizeUtil.px(100),
              height: SizeUtil.px(100),
              fit: BoxFit.fill),
        ),
        Positioned(
          right: 5,
          top: 5,
          child: GestureDetector(
            onTap: () {
              setState(() {
                images = null;
              });
            },
            child: ClipOval(
              //圆角删除按钮
              child: Container(
                padding: EdgeInsets.all(5),
                decoration: BoxDecoration(color: Colors.black54),
                child: Icon(
                  Icons.close,
                  size: 20,
                  color: Colors.white,
                ),
              ),
            ),
          ),
        ),
      ],
    );
  }

  void _showNickname() {
    final top = 12.0;
    final txBottom = 40.0;
    final txHeight = 100.0;
    TextEditingController nameController = TextEditingController();
    showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      builder: (ctx) {
        return StatefulBuilder(builder: (ctx2, state) {
          return Container(
            height: MediaQuery.of(ctx2).viewInsets.bottom +
                txHeight +
                top +
                txBottom,
            color: Colors.white,
            child: Stack(
              children: <Widget>[
                Positioned(
                  left: 12,
                  bottom: (MediaQuery.of(ctx2).viewInsets.bottom < 0)
                      ? 0
                      : MediaQuery.of(ctx2).viewInsets.bottom,
                  right: 0,
                  top: top,
                  child: Container(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.end,
                          children: <Widget>[
                            Expanded(
                                child: Container(
                              padding: EdgeInsets.all(8),
                              height: txHeight,
                              color: Color(0xfff7f7f7),
                              child: TextField(
                                // scrollPadding: EdgeInsets.zero,
                                controller: nameController,
                                autofocus: true,
                                maxLines: 4,
                                style: TextStyle(
                                    fontSize: 15, color: Colors.black),
                                decoration: InputDecoration(
                                    contentPadding: EdgeInsets.zero,
                                    isDense: true,
                                    border: InputBorder.none),
                              ),
                            )),
                            InkWell(
                              child: Container(
                                padding: EdgeInsets.only(left: 12, right: 12),
                                child: Text("确定",
                                    style: TextStyle(
                                        fontSize: 15,
                                        color: Color(0xffe62d6c))),
                              ),
                              onTap: () {
                                setState(() {
                                  widget.arguments['userInfo'].nickname =
                                      nameController.text;
                                });
                                Navigator.pop(context);
                              },
                            )
                          ],
                        ),
                      ],
                    ),
                  ))
              ],
            ),
          );
        });
      },
    );
  }

  void _showSignature() {
    final top = 12.0;
    final txBottom = 40.0;
    final txHeight = 100.0;
    TextEditingController signatureController = TextEditingController();
    showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      builder: (ctx) {
        return StatefulBuilder(builder: (ctx2, state) {
          return Container(
            height: MediaQuery.of(ctx2).viewInsets.bottom +
                txHeight +
                top +
                txBottom,
            color: Colors.white,
            child: Stack(
              children: <Widget>[
                Positioned(
                  left: 12,
                  bottom: (MediaQuery.of(ctx2).viewInsets.bottom < 0)
                      ? 0
                      : MediaQuery.of(ctx2).viewInsets.bottom,
                  right: 0,
                  top: top,
                  child: Container(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.end,
                          children: <Widget>[
                            Expanded(
                                child: Container(
                                  padding: EdgeInsets.all(8),
                                  height: txHeight,
                                  color: Color(0xfff7f7f7),
                                  child: TextField(
                                    // scrollPadding: EdgeInsets.zero,
                                    controller: signatureController,
                                    autofocus: true,
                                    maxLines: 4,
                                    style: TextStyle(
                                        fontSize: 15, color: Colors.black),
                                    decoration: InputDecoration(
                                        contentPadding: EdgeInsets.zero,
                                        isDense: true,
                                        border: InputBorder.none),
                                  ),
                                )),
                            InkWell(
                              child: Container(
                                padding: EdgeInsets.only(left: 12, right: 12),
                                child: Text("确定",
                                    style: TextStyle(
                                        fontSize: 15,
                                        color: Color(0xffe62d6c))),
                              ),
                              onTap: () {
                                setState(() {
                                  widget.arguments['userInfo'].signature =
                                      signatureController.text;
                                });
                                Navigator.pop(context);
                              },
                            )
                          ],
                        ),
                      ],
                    ),
                  ))
              ],
            ),
          );
        });
      },
    );
  }

  void _showPhone() {
    final top = 12.0;
    final txBottom = 40.0;
    final txHeight = 200.0;
    showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      builder: (ctx) {
        return StatefulBuilder(builder: (ctx2, state) {
          return Container(
            height: MediaQuery.of(ctx2).viewInsets.bottom +
                txHeight +
                top +
                txBottom,
            color: Color(0xff2b244d),
            child: Stack(
              children: <Widget>[
                Positioned(
                  left: 12,
                  bottom: (MediaQuery.of(ctx2).viewInsets.bottom < 0)
                      ? 0
                      : MediaQuery.of(ctx2).viewInsets.bottom,
                  right: 0,
                  top: top,
                  child: Container(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Align(
                          alignment: Alignment.topCenter,
                          child: Padding(
                            padding: EdgeInsets.only(left: SizeUtil.px(60),right: SizeUtil.px(60)),
                            child: MyTextField(
                              focusNode: _nodeName,
                              placeHolder: '请输入您的手机号',
                              maxLength: 11,
                              keyboardType: TextInputType.phone,
                              controller: _nameController,
                              onChanged: (text){
                                phone = text;
                                checkInput();
                              },
                            ),
                          ),
                        ),
                        Align(
                          alignment: Alignment.topCenter,
                          child: Padding(
                            padding: EdgeInsets.only(left: SizeUtil.px(60),right: SizeUtil.px(60),top: SizeUtil.px(30)),
                            child: MyTextField(
                              focusNode: _nodeVCode,
                              placeHolder: '请输入验证码',
                              maxLength: 6,
                              controller: _vcodeController,
                              getVCode: (text){
                                code = text;
                              },
                              keyboardType: TextInputType.phone,
                              onChanged: (text){
                                code = text;
                                checkInput();
                              },
                            ),

                          ),
                        ),
                        Align(
                          alignment: Alignment.topCenter,
                          child: Padding(
                              padding: EdgeInsets.only(left: SizeUtil.px(60),right: SizeUtil.px(60),top: SizeUtil.px(60)),
                              child: Container(
                                width: SizeUtil.px(650),
                                height: SizeUtil.px(91),
                                child: RaisedButton(
                                  shape: RoundedRectangleBorder(
                                    side: BorderSide.none,
                                    borderRadius: BorderRadius.all(Radius.circular(30)),
                                  ),
                                  color: Color(0xffe62d6c),
                                  disabledColor: Color(0xffe62d6c),
                                  child: Text(
                                    '绑定手机',
                                    style: TextStyle(fontSize: SizeUtil.px(36),color: Color(0xffffffff)),
                                  ),
                                  onPressed: (){
                                    checkInput();
                                    if(resetPhont){
                                      setState(() {
                                        send();
                                      });
                                    }else{
                                      setState(() {
                                        TxUtils.showErrorToast('您填写的信息不完整', context);
                                      });
                                    }
                                  },
                                ),

                              )
                          ),
                        ),
                      ],
                    ),
                  ))
              ],
            ),
          );
        });
      },
    );
  }

  void checkInput() {
    bool enable;
    if (isNotEmpty(phone) && isNotEmpty(code) ) {
      enable = true;
    } else {
      enable = false;
    }
    setState(() {
      resetPhont = enable;
    });
  }

  updateUserInfo() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    PhotoWall photoWall = widget.arguments['userInfo'].photoWall;
    if(image != null && image.length > 0){
      Map<String, dynamic> map = Map();
      FormData formData;
      for(int i = 0 ; i < image.length; i ++){
        if(i == 0){
          if(photoWall.photo1 == null || photoWall.photo1 == ''){
            map['photo' + (i+1).toString()] = await MultipartFile.fromFile(image[i].path);
          }
        }
        if(i == 1){
          if(photoWall.photo2 == null || photoWall.photo2 == ''){
            map['photo' + (i+1).toString()] = await MultipartFile.fromFile(image[i].path);
          }
        }
        if(i == 2){
          if(photoWall.photo3 == null || photoWall.photo3 == ''){
            map['photo' + (i+1).toString()] = await MultipartFile.fromFile(image[i].path);
          }
        }
        if(i == 3){
          if(photoWall.photo4 == null || photoWall.photo4 == ''){
            map['photo' + (i+1).toString()] = await MultipartFile.fromFile(image[i].path);
          }
        }
        if(i == 4){
          if(photoWall.photo5 == null || photoWall.photo5 == ''){
            map['photo' + (i+1).toString()] = await MultipartFile.fromFile(image[i].path);
          }
        }

      }
      formData = FormData.fromMap(map);
      var dio = new Dio();
      dio.options.headers = {"usertoken": appToken};
      var response =
      await dio.post("http://119.28.131.94:82/app/user/photo/wall/add", data: formData);
      var result = response.data;
      if (result['code'] == -1) {
        TxUtils.showErrorToast('登录失效，请重新登录', context);
        Navigator.pop(context);
        Navigator.of(context).push(MaterialPageRoute(
          builder: (context) {
            return LoginPage(
              arguments: {'loginCode': '-1'},
            );
          },
        ));
      } else if (result['code'] == 0) {
        TxUtils.showToast(result['data'], context);
        Navigator.of(context).pop("refresh");
      }
    }
    Map<String, dynamic> map = Map();
    FormData formData;
    if (images != null) {
      map['avatar'] = await MultipartFile.fromFile(images.path);
    }
    if (widget.arguments['userInfo'].nickname != '') {
      map['nickname'] = widget.arguments['userInfo'].nickname;
    }
    if (widget.arguments['userInfo'].sex != 0) {
      map['sex'] = widget.arguments['userInfo'].sex;
    }
    if(widget.arguments['userInfo'].signature != ''){
      map['signature'] = widget.arguments['userInfo'].signature;
    }
    if(widget.arguments['userInfo'].birthday != ''){
      map['birthday'] = widget.arguments['userInfo'].birthday;
    }
    formData = FormData.fromMap(map);

    var dio = new Dio();
    dio.options.headers = {"usertoken": appToken};
    var response =
        await dio.post("http://119.28.131.94:82/app/user/edit", data: formData);
    var result = response.data;
    if (result['code'] == -1) {
      TxUtils.showErrorToast('登录失效，请重新登录', context);
      Navigator.pop(context);
      Navigator.of(context).push(MaterialPageRoute(
        builder: (context) {
          return LoginPage(
            arguments: {'loginCode': '-1'},
          );
        },
      ));
    } else if (result['code'] == 0) {
      TxUtils.showToast(result['data'], context);
      Navigator.of(context).pop("refresh");
    }
  }

  Future<void> send() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    String appToken = prefs.getString('app-token');
    BaseRequest request = ModifyPhontRequest();
    request.add('phone', phone).add('code', code).addHeader('usertoken', appToken);
    var result = await HiNet.getInstance().fire(request);
    if (result['code'] == -1) {
      TxUtils.showErrorToast('登录失效，请重新登录', context);
      Navigator.pop(context);
      Navigator.of(context).push(MaterialPageRoute(
        builder: (context) {
          return LoginPage(arguments: {'loginCode': '-1'},);
        },
      ));
    }else if(result['code'] == 0){
      TxUtils.showToast('修改成功', context);
      setState(() {
        widget.arguments['userInfo'].phone = phone;
        _nameController.text = '';
        _vcodeController.text = '';
      });
      Navigator.pop(context);
    }
  }

  void getInitPic() {
    PhotoWall photoWall = widget.arguments['userInfo'].photoWall;
    if(photoWall != null){
      if(photoWall.photo1 != '' && photoWall.photo1 != null){
        image.add(new File('http://119.28.131.94:82' + photoWall.photo1));
      }
      if(photoWall.photo2 != '' && photoWall.photo2 != null){
        image.add(new File('http://119.28.131.94:82' + photoWall.photo2));
      }
      if(photoWall.photo3 != '' && photoWall.photo3 != null){
        image.add(new File('http://119.28.131.94:82' + photoWall.photo3));
      }
      if(photoWall.photo4 != '' && photoWall.photo4 != null){
        image.add(new File('http://119.28.131.94:82' + photoWall.photo4));
      }
      if(photoWall.photo5 != '' && photoWall.photo5 != null){
        image.add(new File('http://119.28.131.94:82' + photoWall.photo5));
      }
    }


  }

}
